<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/controller/interface.php';
require_once $basedir . '/../../../classes/controller/abstract.php';
require_once $basedir . '/../../../classes/controller/atom.php';

require_once $basedir . '/../../../classes/atom.php';

require_once $basedir . '/../../../classes/minime/atom/loader.php';

require_once 'vfsStream/vfsStream.php';

class Controller_AtomTest extends PHPUnit_Framework_TestCase {
    
    public function testConstructorOfAtomController() {
        
        $_GET['username'] = "tuomas";
        $_GET['controller'] = "atom";
        $_POST[] = array();

        $conf['datastore']['user'] = vfsStream::url('srv/datastorage');
        
        $obj = new Controller_Atom(array(), array(), "", $conf);
        $this->assertEquals('Controller_Atom', get_class($obj));
        
    }
    
public function testAtomControllerSuccessWorkflow() {
        
        $_GET['username'] = "tuomas";
        $_GET['controller'] = "atom";
        $_POST[] = array();
        
        $expected = '<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <generator uri="http://madebymonsieur.com" version="0.0.1">MiniMe - Life to XML</generator>
  <id>http://www.lobstermonster.org/tuomas</id>
  <title>This is a magic feed!</title>
  <subtitle>Very amazing feed!</subtitle>
  <logo>http://www.example.com/logo.png</logo>
  <updated>2010-07-22T23:25:59+00:00</updated>
  <author>
    <name>Tuomas Koski</name>
    <uri>http://www.lobstermonster.org/tuomas</uri>
  </author>
  <link rel="alternate" type="text/html" href="http://www.lobstermonster.org/tuomas"/>
  <link rel="hub" href="http://www.lobstermonster.org/minime/hub.php"/>
  <link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://www.lobstermonster.org/minime/salmon/tuomas"/>
  <link rel="self" type="application/atom+xml" href="http://www.lobstermonster.org/minime/atom/tuomas"/>
  <activity:subject>
    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
    <id>tuomas@lobstermonster.org</id>
    <title>Tuomas Koski</title>
    <link rel="alternate" type="text/html" href="http://www.lobstermonster.org/tuomas"/>
  </activity:subject>
  <entry>
    <id>mimime:12345689</id>
    <title>Hey! What\'s up?</title>
    <content type="text/html">Hey! What\'s up?</content>
    <updated>2010-07-28T06:42:55+02:00</updated>
    <author>
      <uri>http://lobstermonster.org/pamela</uri>
      <name>Pamela A</name>
    </author>
    <activity:actor>
      <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
      <id>pamela@lobstermonster.org</id>
      <title>Pamela A</title>
      <link rel="alternate" type="text/html" href="http://identi.ca/pamela"/>
    </activity:actor>
    <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  </entry><entry>
    <id>mimime:12345689</id>
    <title>Tuomas is now following Pamela Anderson</title>
    <content type="text/html">Tuomas is now following Pamela Anderson</content>
    <updated>2010-07-26T06:42:55+02:00</updated>
    <author>
      <uri>http://lobstermonster.org/tuomas</uri>
      <name>Tuomas Koski</name>
    </author>
    <activity:actor>
      <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
      <id>tuomas@lobstermonster.org</id>
      <title>Tuomas Koski</title>
      <link rel="alternate" type="text/html" href="http://identi.ca/tkoski"/>
    </activity:actor>
    <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
  </entry>
</feed>
';
        
        # Register our virtual file system
        vfsStreamWrapper::register();
        
        # Create a new root directory
        $root = new vfsStreamDirectory('srv');
        $root->addChild(new vfsStreamDirectory('datastorage'));
        $root->getChild('datastorage')->addChild(new vfsStreamDirectory('user'));
        $root->getChild('datastorage')->getChild('user')->addChild(new vfsStreamDirectory('tuomas'));
        $root->getChild('datastorage')->getChild('user')->getChild('tuomas')->addChild(vfsStream::newFile('atom.xml')->withContent($expected));
        vfsStreamWrapper::setRoot($root);
       
        $conf['datastore']['user'] = vfsStream::url('srv/datastorage/user');
        
        $controller = new Controller_Atom($_GET, $_POST, "", $conf);
        $controller->execute();
        
        $this->assertEquals($expected, $controller->getOutputBody());
        $this->assertArrayHasKey('Content-Type', $controller->getHeaders());
        $this->assertContains(Atom::CONTENT_TYPE, $controller->getHeaders());
    }
}